package cn.ecut.servlet;

import cn.ecut.dao.UserDao;
import cn.ecut.model.User;
import cn.ecut.services.UserServices;
import cn.ecut.util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

@WebServlet("/userLogin")
public class UserLogin extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        HttpSession session = request.getSession();
        User user = new User();
        user.setUserName(username);
        user.setPassword(password);

        String msg="";
        UserServices userServices = new UserServices();
        try {
            user = userServices.loadUsername(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (user!=null){
            if (user.getPassword().equals(password)){
                response.sendRedirect("/index.jsp");
                session.setAttribute("user",user);
            }else {
                msg="用户名或密码错误";
            }
        }else {
            msg="此用户不存在";
        }
        request.setAttribute("msg",msg);
        request.getRequestDispatcher("/login.jsp").forward(request,response);
    }
}
